package com.onlineClass.edu.controller;

import cn.hutool.core.collection.CollUtil;
import com.onlineClass.edu.base.BaseApiController;
import com.onlineClass.edu.base.BaseResponse;
import com.onlineClass.edu.constant.LocalCacheKeyConstants;
import com.onlineClass.edu.entity.EduCourse;
import com.onlineClass.edu.entity.dto.response.EduCourseRespDto;
import com.onlineClass.edu.entity.requestDto.EduCourseReqDto;
import com.onlineClass.edu.service.IEduCourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程表 前端控制器
 * </p>
 *
 * @author denghui
 * @since 2024-05-01
 */
@Controller
@RequestMapping("/eduCourse")
@ResponseBody
@Slf4j
public class EduCourseController extends BaseApiController {
    @Autowired
    private IEduCourseService iEduCourseService;
    @GetMapping("/searchCourseList")
    public BaseResponse searchCourseList(EduCourseReqDto reqDto){
        List<EduCourseRespDto> eduCourses = iEduCourseService.searchFromCache(reqDto);
        if (CollectionUtils.isEmpty(eduCourses)){
            log.error("[未查询到数据]");
            return setResultError("未查询到数据");
        }
        //进行分页
        Integer pageNo = reqDto.getPageNo();
        int totalPage = (int) Math.ceil(eduCourses.size() / (double) LocalCacheKeyConstants.DEFAULT_PAGE_SIZE);
        if (pageNo>totalPage){
            log.error("[分页查询失败]");
            return setResultError("分页查询失败");
        }
        //分页后的数据
        List<EduCourseRespDto> paged = CollUtil.page(pageNo - 1, LocalCacheKeyConstants.DEFAULT_PAGE_SIZE, eduCourses);
        // 返回最终数据
        HashMap result = new HashMap();
        result.put("pageNo", pageNo); //当前页面
        result.put("pageSize", LocalCacheKeyConstants.DEFAULT_PAGE_SIZE); // 每页条数
        result.put("total",  eduCourses.size()); // 总记录数
        result.put("totalPage", totalPage); // 总页数
        result.put("listPageEduCourses", paged); // 分页后数据
        return setResultSuccessData(result);
    }



}
